from rest_framework.generics import ListCreateAPIView
from meiduo_admin.serializers.users import UserSerializer, UserAddSerializer
from meiduo_admin.utils import UserPageNum
from users.models import User

class UserView(ListCreateAPIView):
    #  指定序列化器
    # serializer_class = UserSerializer
    #  指定分页器
    pagination_class = UserPageNum

    #  根据不同的请求方式返回不同的序列化器
    def get_serializer_class(self):
        #  请求方式为get，则是获取用户数据返回UserSerializer
        if self.request.method == 'GET':
            return UserSerializer
        else:
            #  POST请求，完成保存用户，返回UserAddSerializer
            return UserAddSerializer


    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:
            return User.objects.all()
        else:
            return User.objects.filter(username=keyword)
